package com.knight.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.knight.common.constant.Constant;
import com.knight.exception.RequestParameterException;
import com.knight.model.Content;
import com.knight.model.Menu;
import com.knight.model.Message;
import com.knight.model.Project;
import com.knight.service.ContentService;
import com.knight.service.MenuService;
import com.knight.service.MessageService;
import com.knight.service.ProjectsService;
import com.knight.service.ServiceFlowService;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法 详见 JFinal 俱乐部:
 * http://jfinal.com/club
 * 
 * IndexController
 */
public class IndexController extends Controller {

	private static MenuService menuService = MenuService.getInstance();
	private static ProjectsService projectsService = ProjectsService.getInstance();
	private static ContentService contentService = ContentService.getInstance();
	private static ServiceFlowService serviceFlowService = ServiceFlowService.getInstance();
	private static MessageService messageService = MessageService.getInstance();
	
	public void index() {
		setAttr("currentMenu", Constant.TopMenusName.INDEX);
		// 查询公司动态
		Page<Content> contentList = contentService.findByTypeForPage(1, 4, "00011");
		setAttr("contents", contentList);
		// 查询项目动态
		Page<Content> projectDynamicList = contentService.findByTypeForPage(1, 4, "00012");
		setAttr("projectDynamics", projectDynamicList);
		// 查询案例
		Page<Project> projectList = projectsService.findProjectByMenuId(1, 4, "00015");
		setAttr("projects", projectList);
		//查询客户留言
		List<Message> messages = messageService.findMessageByStatus();
		setAttr("messages", messages);
		
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,Constant.ParentMenuId.PROJECTS_ID);
		setAttr("projectMenus", list);
		render("index.html");
	}

	/**
	 * 关于我们
	 * 
	 * @throws Exception
	 */
	public void about() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.ABOUT_ID);

		String requestData = getPara();
		Map<String, String> map = commonMethod(list, requestData, this);

		// TODO 测试使用
		setAttr("flag", map.get("id"));
		if (map.get("id").equals("00010")) {
			render("aboutCompany/culture.html");
		} else {
			render("aboutCompany/about.html");
		}
	}

	/**
	 * 新闻中心
	 * 
	 * @throws Exception
	 */
	public void news() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.NEWS_ID);

		String requestData = getPara();
		Map<String, String> map = commonMethod(list, requestData, this);
		String id = map.get("id");

		Page<Content> contents = contentService.findByTypeForPage(getParaToInt(1, 1), 9, id);
		setAttr("contents", contents);
		setAttr("pageUrl", "/news/" + id);
		render("news/news.html");
	}

	/**
	 * 资质荣誉
	 * 
	 * @throws Exception
	 */
	public void shidaifong() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.HONORS_ID);
		String requestData = getPara();
		Map<String, String> map = commonMethod(list, requestData, this);
		// TODO 测试使用
		setAttr("flag", map.get("id"));
		render("shidaifong/shidaifong.html");
	}

	/**
	 * 公共方法
	 * 
	 * @param list
	 * @param requestData
	 * @param controller
	 * @return
	 * @throws Exception
	 */
	private Map<String, String> commonMethod(List<Menu> list, String requestData, Controller controller)
			throws Exception {
		if (list == null || list.size() == 0) {
			throw new RequestParameterException(Constant.ExceptionMessage.PARAMETER_IS_NULL);
		}
		Menu firstMenu = list.get(0);
		String id = null;
		if (requestData != null) {
			id = controller.getPara(0);
		} else {
			firstMenu.getParentId();
			id = firstMenu.get("id").toString();
		}
		controller.setAttr("menus", list);
		Menu m = menuService.findByPrimaryKey(firstMenu.getParentId());
		controller.setAttr("secondMenuName", m.getName());

		Map<String, String> map = new HashMap<String, String>();

		map.put("id", id);

		return map;
	}

	/**
	 * 案例
	 */
	public void projects() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.PROJECTS_ID);

		String requestData = getPara();
		Map<String, String> map = commonMethod(list, requestData, this);
		String id = map.get("id");

		Page<Project> projects = projectsService.findProjectByMenuId(getParaToInt(1, 1), 9, id);
		setAttr("menus", list);
		setAttr("pageUrl", "/projects/" + id);
		setAttr("projects", projects);
		render("project/anli.html");
	}

	/**
	 * 服务流程
	 * 
	 * @throws Exception
	 */
	public void serviceflow() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.SERVICEFLOW_ID);
		String requestData = getPara();

		Map<String, String> map = commonMethod(list, requestData, this);
		String id = map.get("id");
		setAttr("serviceUrl", serviceFlowService.getServiceFlow(id));
		render("serviceFlow/serviceflow.html");
	}

	/**
	 * 客户留言
	 * 
	 * @throws Exception
	 */
	public void customermessage() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.CUSTOMERMESSAGE_ID);

		String requestData = getPara();
		Map<String, String> map = commonMethod(list, requestData, this);
		render("customermessage/customermessage.html");
	}

	/**
	 * 联系我们
	 * 
	 * @throws Exception
	 */
	public void contactus() throws Exception {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.CONTACTUS_ID);

		String requestData = getPara();
		Map<String, String> map = commonMethod(list, requestData, this);

		render("contactus/contact.html");
	}
}
